package com.tiantian.quanminxiu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.jakewharton.rxbinding.view.RxView;
import com.shenglian.utils.utils.CommonUtils;
import com.umeng.socialize.UMShareAPI;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.presenter.LoginPresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.view.LoginView;
import com.tiantian.quanminxiu.wxapi.WXEntryActivity;

import rx.functions.Action1;

/**
 * Created by admin on 2016/9/18.
 */
public class Activity_Login extends ActivityBase implements LoginView{

    private UMShareAPI mShareAPI;

    private LoginPresent present;
    private EditText etLoginUserAccount,etLoginUserPassword;
    private View vLoginSub,vLoginQQ,vLoginWeixin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Init();

        InitView();
        mShareAPI = UMShareAPI.get(thisActivity);
        present = new LoginPresent();
        present.attachView(this);
    }

    @Override
    public void InitView() {
        vLoginSub = findViewById(R.id.vLoginSub);
        vLoginQQ = findViewById(R.id.vLoginQQ);
        vLoginWeixin = findViewById(R.id.vLoginWeixin);

        etLoginUserAccount = (EditText) findViewById(R.id.etLoginUserAccount);
        etLoginUserPassword = (EditText) findViewById(R.id.etLoginUserPassword);

        RxView.clicks(vLoginSub).map(View->vLoginSub).subscribe(OC_Button);
        RxView.clicks(vLoginQQ).map(View->vLoginQQ).subscribe(OC_Button);
        RxView.clicks(vLoginWeixin).map(View->vLoginWeixin).subscribe(OC_Button);
        RxView.clicks(mHeader.txtHeader_Back).map(View->mHeader.txtHeader_Back).subscribe(OC_Button);
    }

    public Action1<View> OC_Button = v->{
        switch (v.getId()){
            case R.id.vLoginSub:
                LoginAccount();
                break;
            case R.id.vLoginQQ:
                CommonUtils.showToask(thisContext,getResources().getString(R.string.not_yet_completed));
//                Loading_Open(true);
//                JumpLoginQQ();
                break;
            case R.id.vLoginWeixin:
                Loading_Open(true);
                JumpLoginWeixin();
                break;
            case R.id.txtHeader_Back:
                //跳转选择登陆页面
                APPUtils.LoginSelect_To(thisActivity);
                finish();
                break;
        }
    };


    @Override
    public void PageInit() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        PageInit();
    }
	
	//region 返回监听 onBackPressed
	@Override
	public void onBackPressed() {
		APPUtils.LoginSelect_To(thisActivity);
		finish();
	}
	//endregion

    @Override
    public void JumpLoginQQ() {
        APPUtils.QQHelperLogin_To(this);
        Activity_QQLoginHelper.setQQLoginHelperBackCall(new Activity_QQLoginHelper.QQLoginHelperBackCall() {
            @Override
            public void backcall(String openid, String access_token, String expires_in, String refresh_token, String nickname, String sex) {
                present.loginQQ(openid,access_token,expires_in,refresh_token,nickname,sex);
            }

            @Override
            public void onError(String msg) {
                Activity_Login.this.Loading_Close();
            }

        });
    }

    @Override
    public void JumpLoginWeixin() {
        WXEntryActivity.startWXlogin(thisContext, new WXEntryActivity.WXEntryBackCall() {
            @Override
            public void backcall(String openid ,String unionid, String access_token,String expires_in,String refresh_token,String nickname,String sex) {
                present.loginWeixin(openid, unionid,access_token,
                        expires_in, refresh_token, nickname, sex);
            }
            @Override
            public void onError(String msg) {
                Activity_Login.this.Loading_Close();
            }
        });
    }


    @Override
    public void LoginAccount() {
        present.login(etLoginUserAccount.getText().toString(),etLoginUserPassword.getText().toString());
    }

    @Override
    public void JumpMain() {
        APPUtils.Main_To(thisActivity);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }
}
